package com.shouyun.modules.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.shouyun.common.exception.ErrorCode;
import com.shouyun.common.utils.DataUtils;
import com.shouyun.common.utils.Result;
import com.shouyun.common.validator.AssertUtils;
import com.shouyun.common.validator.ValidatorUtils;
import com.shouyun.modules.security.user.SecurityUser;
import com.shouyun.modules.security.user.UserDetail;
import com.shouyun.modules.sys.dto.PasswordDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 *
 * @author Sunshouguo
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    /**
     * 列表
     * @param params
     * @return
     */
    @GetMapping("list")
    public Result list(@RequestParam Map<String, Object> params) {
        JSONObject userAll = DataUtils.getData(DataUtils.USER);
        List<Object> userList = new ArrayList<>();
        String keyword = null;
        if (!ObjectUtils.isEmpty(params.get("keyword"))) {
            keyword = params.get("keyword").toString();
        }
        for (Map.Entry<String, Object> userJSONObject : userAll.entrySet()) {
            String username = StringUtils.isNotBlank(userJSONObject.getKey()) ? userJSONObject.getKey() : "";
            if (StringUtils.isBlank(keyword) || (StringUtils.isNotBlank(keyword) && username.contains(keyword))) {
                userList.add(userJSONObject.getValue());
            }
        }
        return new Result().ok(userList);
    }

    /**
     * 获取用户信息
     * @param username
     * @return
     */
    @GetMapping("{username}")
    public Result get(@PathVariable("username") String username) {
        return new Result().ok(DataUtils.getData(DataUtils.USER, username));
    }

    /**
     * 登录用户信息
     * @return
     */
    @GetMapping("info")
    public Result info() {
        return new Result().ok(SecurityUser.getUser());
    }

    /**
     * 修改密码
     * @param dto
     * @return
     */
    @PutMapping("password")
    public Result password(@RequestBody PasswordDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto);

        UserDetail user = SecurityUser.getUser();

        //原密码不正确
        if (dto.getPassword() == null || !dto.getPassword().equals(user.getPassword())) {
            return new Result().error(ErrorCode.PASSWORD_ERROR);
        }

        JSONObject userData = new JSONObject();
        userData.put("username", dto.getUsername());
        userData.put("password", dto.getNewPassword());
        userData.put("superAdmin", user.getSuperAdmin());
        DataUtils.setData(DataUtils.USER, dto.getUsername(), userData);
        return new Result();
    }

    /**
     * 保存
     * @param user
     * @return
     */
    @PostMapping
    public Result save(@RequestBody JSONObject user) {
        String username = user.getString("username");
        String password = user.getString("password");
        Integer superAdmin = user.getInteger("superAdmin");

        //效验数据
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return new Result().error("缺少参数");
        }
        JSONObject userData = new JSONObject();
        userData.put("username", username);
        userData.put("password", password);
        userData.put("superAdmin", superAdmin);
        DataUtils.setData(DataUtils.USER, username, userData);
        return new Result();
    }

    /**
     * 删除
     * @param userNames
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody String[] userNames) {
        //效验数据
        AssertUtils.isArrayEmpty(userNames, "username");
        for (String userName : userNames) {
            DataUtils.removeData(DataUtils.USER, userName);
        }
        return new Result();
    }

}